package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Movies;
import com.itheima.result.Result;
import com.itheima.service.BannerService;
import com.itheima.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("movies")//作为当前类中所有请求的前缀
public class MovieController {

    @Autowired
    private MovieService movieService;

    @Autowired
    private BannerService bannerService;

    @GetMapping("list")
    //@RequestParam单独使用时意味请求中必须携带该参数且有值
    public Result findByPage(
           @RequestParam Integer pageNum,
           @RequestParam Integer pageSize,
            Integer cid,
            String keyword){
        //调用service完成分页条件查询
        PageInfo pageInfo = movieService.findByPage(pageNum,pageSize,cid,keyword);

        return Result.success(pageInfo);
    }

    @DeleteMapping("deleteByIds")
    public Result deleteBatch(Integer[] ids){
        //调用service完成批量删除操作
        movieService.deleteBatch(ids);
        return Result.success();
    }

    @DeleteMapping("deleteById")
    public Result deleteById(@RequestParam Integer id){
        //调用service完成批量删除操作
        Integer[] ids = {id};
        movieService.deleteBatch(ids);
        return Result.success();
    }
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody Movies movies){
        //判断传入影视id是否为空，为空则为添加影视，反之为修改影视
        if (StringUtils.isEmpty(movies.getId())){
            movieService.add(movies);
            return Result.success();
        }
        movieService.update(movies);
        return Result.success();

    }

    @GetMapping("findById")
    public Result getById(@RequestParam Integer id){
        //调用service完成根据id查找影视操作
        Movies movie = movieService.findById(id);
        return Result.success(movie);
    }

    @PostMapping("save2Banner")
    public Result save2Banner(@RequestParam Integer id){
        //将影片放置轮播
        Movies movie = movieService.findById(id);
        bannerService.add(movie);
        return Result.success();
    }
}
